package com.inxedu.os.edu.controller.system;

import com.google.gson.Gson;

import com.inxedu.os.common.controller.BaseController;
import com.inxedu.os.edu.pojo.SysFunction;
import com.inxedu.os.edu.service.system.SystemFounctionService;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统权限
 */
@Controller
@RequestMapping(value = "/admin/sysfunctioin")

@Api(value = "权限管理页面Controller",tags = "权限管理")
public class SystemFunctionController  extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(SystemFunctionController.class);

    @InitBinder({"sysFunction"})
    public void initBinderSysFunction(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("sysFunction.");
    }
    @Autowired
    private SystemFounctionService systemFounctionService;

    Gson gson = new Gson();
    /**
     * 权限树的显示
     * @return
     */
    @ApiOperation( "显示权限树")
    @ApiImplicitParam(name = "HttpServletRequest",value = "接收一个Request")
    @RequestMapping(value = "/showfunctionztree",method = RequestMethod.GET)
    public String  showSystenFunctionTree(HttpServletRequest httpServletRequest){
        List<SysFunction> systrmFunctionAll = systemFounctionService.findSystrmFunctionAll();

        String json = gson.toJson(systrmFunctionAll);
        httpServletRequest.setAttribute("jsonFunction",json);
        return "view/inxedu/admin/system/sys-function-list";
    }

    /**
     * 添加权限
     *
     * @param sysFunction
     * @return
     */
    @ApiOperation("添加权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "接收一个parentId！",dataType = "Integer"),
            @ApiImplicitParam(name = "functionId", value = "接收一个functionId！",dataType = "Integer"),

    })    @RequestMapping(value = "/addfunction",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addSystemFunction(@ModelAttribute("sysFunction") SysFunction sysFunction){
        System.out.println(sysFunction);
        Gson gson = new Gson();
        Map<String,Object> json=new HashMap<>();
        try{
        sysFunction.setCreateTime(new Date());
        sysFunction.setFunctionType(1);
        sysFunction.setFunctionName("新建权限分支");
        sysFunction.setSort(0);
        int i = systemFounctionService.addSystemFunction(sysFunction);


        if (i>0){
            json=this.setJson(true,"新建权限成功！",null);//sysFunction.getParentId()
        }else {
            json=this.setJson(false,"新建权限失败！！",null);
        }

        }
        catch (Exception e){
            logger.error("新建分支失败！");
        }

        return json;
    }

    /**
     * 删除
     * @param ids
     * @return
     */

    @ApiOperation("删除权限")
    @ApiImplicitParam(name = "ids" ,value = "接收一个ids节点id字符串！",dataType="String")
    @RequestMapping(value = "/deletefunction/{ids}",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> deleteSystemFunctionByid(@PathVariable("ids")  String  ids ){

        Map<String,Object> json=new HashMap<>();
        try {

            System.out.println(ids);
            int i=systemFounctionService.deleteSystemFunctionByid(ids);
            if(i>0){
                json=this.setJson(true,"删除成功！",null);
                System.out.println("删除成功！");
            }else {
                json=this.setJson(false,"删除失败！",null);
            }

        }catch (Exception e){
            logger.error("删除失败！");
        }

        return json;

    }

    /**
     *  修改
     * @param sysFunction
     * @return
     */
    @ApiOperation("修改权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "functionName", value = "接收一个functionName！",dataType = "String"),
            @ApiImplicitParam(name = "functionId", value = "接收一个functionId！",dataType = "Integer"),
            @ApiImplicitParam(name = "functionUrl", value = "接收一个functionId！",dataType = "String"),
            @ApiImplicitParam(name = "functionType", value = "接收一个functionId！",dataType = "Integer"),
            @ApiImplicitParam(name = "sort", value = "接收一个functionId！",dataType = "Integer")

    })
    @RequestMapping(value = "/updatefunction",method = RequestMethod.POST)
    @ResponseBody
    public  Map<String,Object> updateSystemFunction(@ModelAttribute("sysFunction") SysFunction sysFunction){

        Map<String,Object> json=new HashMap<>();
        System.out.println(sysFunction);
        int i=systemFounctionService.updateSystemFunction(sysFunction);
        if (i>0){
            json=this.setJson(true,"修改成功！",sysFunction);
        }
        else {
            json=this.setJson(false,"修改失败！",sysFunction);
        }
        return json;
    }


    /**
     * 拖拽修改节点
     */

    @ApiOperation("拖拽修改权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "接收一个parentId！"),
            @ApiImplicitParam(name = "functionId", value = "接收一个functionId！")
    })
    @RequestMapping(value = "/updateparentid/{parentId}/{functionId}",method = RequestMethod.POST)
    @ResponseBody
    public  Map<String,Object> updateSystemFunctionByid(@PathVariable("parentId")  Integer parentId,@PathVariable ("functionId") Integer functionId ) {
        Map<String, Object> json = new HashMap<>();

        int i=systemFounctionService.updateSystemFunctionByid(parentId,functionId);
        if (i > 0) {
            json=this.setJson(true,"修改成功！",null);
        }else {
            json=this.setJson(false,"修改失败！",null);
        }
        return json;
    }
}
